// ================================================================================
// 		File: PartyForumType.cs
// 		Desc: 
//  
// 		Called by:   
//               
// 		Auth: chenwenzhi（）
// 		Date: 2016-05-05 21:55
// ================================================================================
// 		Change History
// ================================================================================
// 		Date:		Author:				Description:
// 		--------	--------			-------------------
//    
// ================================================================================
// 
// ================================================================================
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using ZHDJ.Core;
using ZHDJ.Data.Models.Box;

namespace ZHDJ.Data.Models.Forum
{
    /// <summary>
    /// 论坛版块
    /// </summary>   
    public class PartyForumType : EntityBase,ICustomTree, IKeyID, ICloneable
    {
        #region 属性

        /// <summary>
        /// 父ID
        /// </summary>
        public string ParentID { get; set; }

        /// <summary>
        /// 版块名
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// 版块图标
        /// </summary>
        public string ImageUrl { get; set; }

        /// <summary>
        /// 描述
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// 排序号
        /// </summary>
        public int OrderNo { get; set; }

        /// <summary>
        /// 是否显示
        /// </summary>
        public bool IsShowIndex { get; set; }



        /// <summary>
        /// 菜单在树形结构中的层级（从0开始）
        /// </summary>
        [NotMapped]
        public int TreeLevel { get; set; }

        /// <summary>
        /// 是否可用（默认true）,在模拟树的下拉列表中使用
        /// </summary>
        [NotMapped]
        public bool Enabled { get; set; }

        /// <summary>
        /// 是否叶子节点（默认true）
        /// </summary>
        [NotMapped]
        public bool IsTreeLeaf { get; set; }
        [NotMapped]
        public string Name
        {
            get { return Title; }
            set { Title = value; }
        }

        public object Clone()
        {
            PartyForumType type = new PartyForumType
            {
                ID = ID,
                 Description = Description,
                ImageUrl = ImageUrl,
                 IsShowIndex = IsShowIndex,
                 OrderNo = OrderNo,
                 ParentID = ParentID,
                 Title = Title,
                 Enabled = Enabled,
                 IsTreeLeaf = IsTreeLeaf,
                  TreeLevel= TreeLevel
            };
            return type;
        }

        #endregion

        #region 查询管理

        public static DataSet GetForumTypeByParentId(string parentId, int pageIndex = 0, int pageSize = 20)
        {
            var para = new List<MySqlParameter>
            {
                new MySqlParameter()
                {
                    DbType = DbType.String,
                    ParameterName = "ParentID",
                    Value = parentId
                },
                new MySqlParameter()
                {
                    DbType = DbType.Int32,
                    ParameterName = "Begin",
                    Value = (pageIndex)*pageSize
                },
                new MySqlParameter()
                {
                    DbType = DbType.Int32,
                    ParameterName = "PageSize",
                    Value = pageSize
                }
            };
            var sql = new StringBuilder(@"SELECT SQL_CALC_FOUND_ROWS *,if(IsShowIndex=1,'是','否') as IsShowIndexName FROM tblpartyforumtype 
                            WHERE ParentID=?ParentID  ");
            sql.Append(" ORDER BY OrderNo  LIMIT ?Begin ,?PageSize;SELECT FOUND_ROWS();");
            return MySqlHelper.ExecuteDataSet(sql.ToString(), para.ToArray());
        }

        public static DataSet GetForumTypeAll(string parentId = "")
        {
            var sql = new StringBuilder(@"SELECT  * FROM tblpartyforumtype where 1=1 ");
            if (!string.IsNullOrEmpty(parentId))
            {
                sql.Append(" and ParentID=''");
            }
            sql.Append(" order by  orderno ");
            return MySqlHelper.ExecuteDataSet(sql.ToString());
        }

        public static int GetMaxOrderNo(string parentId)
        {
            StringBuilder sbSql = new StringBuilder();
            List<MySqlParameter> parameters = new List<MySqlParameter>();
            sbSql.Append(@"select ifnull(max(OrderNo),0)+1 as maxorderno from 
                            (select * from tblpartyforumtype where orderno!=999 and orderno!=1001 and parentid=@parentid) as tab");
            parameters.Add(new MySqlParameter() { DbType = DbType.String, ParameterName = "parentid", Value = parentId });

            return Convert.ToInt32(MySqlHelper.ExecuteScalar(sbSql.ToString(), parameters.ToArray()));
        }

        #endregion
    }
}
